﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;

namespace VEXREDUS_hil_interface
{
    public partial class HIL_Settings_Form : Form
    {
        //private HIL_Interface_Config config;
        private HIL_Interface hil;

        int[] baudrates = { 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 112500 };

        public HIL_Settings_Form(HIL_Interface hil_interface)
        {
            hil = hil_interface;
            
            InitializeComponent();
            
            UpdateGUI();
        }

        private void UpdateGUI()
        {
            textBoxXPlaneUDPOutputPort.Text = hil.HILConfig.XPlaneUDPOutputPortNumber.ToString();
            textBoxXPlaneUDPInputIP.Text = hil.HILConfig.XPlaneUDPInputIP.ToString();
            textBoxXPlaneUDPInputPort.Text = hil.HILConfig.XPlaneUDPInputPortNumber.ToString();

            //comboBoxVexredusSerialPortName.Items.AddRange(available_serial_ports);
            string[] available_serial_ports = SerialPort.GetPortNames();

            // If there is no available serial ports, just keep the one from the config (so that config file isn't messed up)
            if (available_serial_ports.Length == 0)
            {
                comboBoxVexredusSerialPortName.Items.Add(hil.HILConfig.VxSerialPortName);
                comboBoxVexredusSerialPortName.SelectedIndex = 0;
            }
            else
            {
                for (int i = 0; i < available_serial_ports.Length; i++)
                {
                    comboBoxVexredusSerialPortName.Items.Add(available_serial_ports[i]);

                    if (available_serial_ports[i] == hil.HILConfig.VxSerialPortName)
                    {
                        comboBoxVexredusSerialPortName.SelectedIndex = i;
                    }
                }
            }

            for (int i = 0; i < baudrates.Length; i++)
            {
                comboBoxVexredusSerialPortBaudrate.Items.Add(baudrates[i].ToString());

                if (baudrates[i] == hil.HILConfig.VxSerialPortBaudrate)
                {
                    comboBoxVexredusSerialPortBaudrate.SelectedIndex = i;
                }
            }
        }

        private void ReadFromGUI()
        {
            hil.HILConfig.XPlaneUDPOutputPortNumber = int.Parse(textBoxXPlaneUDPOutputPort.Text);
            hil.HILConfig.XPlaneUDPInputIP = textBoxXPlaneUDPInputIP.Text;
            hil.HILConfig.XPlaneUDPInputPortNumber = int.Parse(textBoxXPlaneUDPInputPort.Text);

            // Make sure that there's a port present.
            try
            {
                hil.HILConfig.VxSerialPortName = comboBoxVexredusSerialPortName.SelectedItem.ToString();
            }
            catch (System.NullReferenceException ex)
            {
                hil.HILConfig.VxSerialPortName = "COM1";
            }

            hil.HILConfig.VxSerialPortBaudrate = int.Parse(comboBoxVexredusSerialPortBaudrate.SelectedItem.ToString());
        }

        private void buttonLoadDefault_Click(object sender, EventArgs e)
        {
            hil.HILConfigRead();
            UpdateGUI();
        }

        private void buttonSaveAsDefault_Click(object sender, EventArgs e)
        {
            ReadFromGUI();
            hil.HILConfigWrite();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            ReadFromGUI();
            hil.HILConfigUpdateGUI();
            this.Close();
        }
    }
}
